﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using SmartSpace.UI.Silverlight.Dynamic.Windows.Controls;

namespace SmartSpace.UI.Silverlight.Dynamic.Windows
{
    public partial class WfHistoryWindow : ChildWindow
    {
        public Guid WorkflowId { get; set; }
        public Guid DataId { get; set; }

        public WfHistoryWindow(Guid workflowId, Guid dataId)
        {
            InitializeComponent();

            WorkflowId = workflowId;
            DataId = dataId;

            this.Loaded += new RoutedEventHandler(WfHistoryWindow_Loaded);
        }

        void WfHistoryWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
            client.GetWorkflowRecordsCompleted += new EventHandler<GetWorkflowRecordsCompletedEventArgs>(client_GetWorkflowRecordsCompleted);
            client.GetWorkflowRecordsAsync(WorkflowId, DataId);
        }

        void client_GetWorkflowRecordsCompleted(object sender, GetWorkflowRecordsCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                WfHistoryPart startPart = new WfHistoryPart(true);
                this._ctlGridHistory.Children.Add(startPart);

                foreach (var item in e.Result.Data)
                {
                    WfHistoryPart part = new WfHistoryPart(item, true);
                    this._ctlGridHistory.Children.Add(part);

                    if (item.IsActivated)
                    {
                        WfHistoryPart part2 = new WfHistoryPart(item, false);
                        this._ctlGridHistory.Children.Add(part2);
                    }
                }

                WfHistoryPart endPart = new WfHistoryPart(false);
                this._ctlGridHistory.Children.Add(endPart);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

